package com.mingqijia.gassafety.authority.vcc.misc;

import com.gap.basic.base.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import com.mingqijia.gassafety.shared.handler.GlobalAuthenticationException;

/**
 * 系统异常统一拦截处理类
 * @author Damon S.
 */
@Slf4j
@RestControllerAdvice
public final class SecurityExceptionHandler {

    @ResponseBody
    @ExceptionHandler(AuthenticationException.class)
    public CommonResponse<Void> notExpected(AuthenticationException failed) {
        if (failed instanceof com.mingqijia.gassafety.shared.handler.GlobalAuthenticationException &&
                ((GlobalAuthenticationException) failed).isLogStack()) {
            log.error("安全认证异常", failed);
        }
        return CommonResponse.failure(failed.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(AccessDeniedException.class)
    public CommonResponse<Void> notExpected(AccessDeniedException failed) {
        return CommonResponse.failure(failed.getMessage());
    }
}